package com.zzwtec.basedata.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.zzwtec.ResultObject;
import com.zzwtec.SimpleResult;
import com.zzwtec.basedata.bean.Area;
import com.zzwtec.basedata.dao.AreaDao;
import com.zzwtec.basedata.services.AreaService;

@RestController
public class AreaController implements AreaService {
	
	@Resource(name = "areaDao")
	private AreaDao areaDao;
	
	
	public SimpleResult addArea(@RequestBody Area bean) {
		SimpleResult result = new SimpleResult();
		String sql = "insert into area (id,area,deletec,isuse,name,remark) values (?,?,?,?,?,?)";
		Object[] params = new Object[]{bean.getId(),bean.getArea(),bean.getDeletec(),bean.getIsuse(),bean.getName(),bean.getRemark()};
		int row = areaDao.insert(sql, params);
		if(row <= 0){
			result.setCode(-1);
			result.setMsg("添加区域失败");
		}
		return result;
	}

	
	public SimpleResult removeArea(@RequestBody String id) {
		SimpleResult result = new SimpleResult();
		String sql = "delete from area where id = ?";
		Object[] params = new Object[]{id};
		int row = areaDao.delete(sql, params);
		if(row <= 0){
			result.setCode(-1);
			result.setMsg("删除区域失败");
		}
		return result;
	}

	
	public SimpleResult modifyArea(@RequestBody Area bean) {
		SimpleResult result = new SimpleResult();		
		String sql = "update area set area= ?,deletec= ?,isuse= ?,name= ?,remark= ? where id= ?"; 
		Object[] params = new Object[]{bean.getArea(),bean.getDeletec(),bean.getIsuse(),bean.getName(),bean.getRemark(),bean.getId()};;
		int size = this.areaDao.update(sql, params);
		if(size <= 0){
			result.setCode(-1);
			result.setMsg("修改区域失败");
		}	
		return result;
	}

	
	public ResultObject<List<Area>> queryAllArea() {
		ResultObject<List<Area>> result = new ResultObject<List<Area>>();
		List<Area> list = areaDao.selectAll();
		result.setData(list);
		return result;
	}

	
	public ResultObject<Area> queryAreaById(@RequestBody String id) {
		ResultObject<Area> result = new ResultObject<Area>();
		Area bean = areaDao.selectOne(id);		
		result.setData(bean);
		return result;
	}
	
}
